package com.dyz.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dyz.domain.WorkIssue;
import com.dyz.mapper.SysUserMapper;
import com.dyz.mapper.WorkIssueMapper;
import com.dyz.model.Result;
import com.dyz.service.IWorkIssueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 工单记录 服务实现类
 * </p>
 *
 * @author dyz
 * @since 2025-04-19
 */
@Service
public class WorkIssueServiceImpl extends ServiceImpl<WorkIssueMapper, WorkIssue> implements IWorkIssueService {
    @Resource
    private WorkIssueMapper workIssueMapper;
    @Resource
    private SysUserMapper sysUserManager;

    @Override
    public Result<?> getWorkIssueList(Page<WorkIssue> page, String startTime, String endTime, String status) {
        page.addOrder(OrderItem.desc("last_update_time"));
       Page<WorkIssue> workIssuePage = workIssueMapper.getWorkIssueList(page, startTime, endTime, status);
        return Result.success(workIssuePage);
    }

    @Override
    public Result<?> updateAnswer(WorkIssue workIssue) {
        //修改工单状态为已回答
        workIssue.setStatus(2);
       String strUserId= SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        long userId = Long.parseLong(strUserId);
        workIssue.setAnswerUserId(userId);
        workIssue.setAnswerName(sysUserManager.selectUserNameById(userId));
        boolean update = this.updateById(workIssue);
        return update ? Result.success("回复成功") : Result.fail("回复失败");
    }
}
